#!/usr/bin/env python
from lib.format_time import *
from lib.HandleMysql import *
from lib.handle_mongo import *
from lib.switch_code import *
from lib.load_data import *
from jj_conn import *
import sys
import re
import time
reload(sys)
sys.setdefaultencoding('utf-8')


def get_order_info( date ,conn ,num):
    stat_conn = set_ib_conn()
    stat_table = "t_order_info"    
    line_list = []
    
    query = "select id,order_id,user_id from orders where mer_id=1019 and date(finish_time)='%s' and pay_status=1"%date
    items = conn.get_results(query)
    for item in items:
        
        order_code = item[1]
        if order_code == None:
            continue
        
        order_id = item[0]
        user_id = item[2]
        
        cash_amount = 0.0
        discount_amount = 0.0
        refund_cash_amount = 0.0
        refund_discount_amount = 0.0
        #order_code = order_code.replace('topInfo','')
        q = "select order_money,from_account_type,ifnull(refund_money,0) from sub_order where order_id='%s'"%order_id
        its = conn.get_results(q)
        if len(its)>0:
            for it in its:                
                order_amount = it[0]
                amount_type = it[1]
                refund_money = it[2]
                if amount_type == 3:
                    discount_amount += float(order_amount)
                    refund_discount_amount += float(refund_money)
                else:
                    cash_amount += float(order_amount)
                    refund_cash_amount += float(refund_money)
                    
        param = [order_code,user_id,order_id,num,cash_amount,discount_amount,refund_cash_amount,refund_discount_amount,date]
        line_list.append('\t'.join(map(lambda i : str(i), param)))
        #print  param;
        
    cont = '\n'.join(line_list)
    cont += '\n'
    path = "/work3/opt/analytic/lh_tmp_data/zhineng/order/"
    file_name = "zn_order_info_%s.txt"%date.replace('-',"")
    writeTextToFile(path,file_name,"w",cont)
    
    load_data(stat_conn,path+file_name,stat_table,'\t')    
    
    conn.close()
    stat_conn.close()


def execute(date):
    try:
        pass
        order_conn = set_order0_conn()
        get_order_info(date,order_conn,0)
        order_conn = set_order1_conn()
        get_order_info(date,order_conn,1)
        order_conn = set_order2_conn()
        get_order_info(date,order_conn,2)
        order_conn = set_order3_conn()
        get_order_info(date,order_conn,3)
    except Exception,e:
        print 'get_order_info:',e
        
def main():
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        execute( stat_date)
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)


if __name__=='__main__':
    main()